<?php
/**
 * --------------------------------------------------------------
 * 用户登录控制器
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

class LoginController extends AdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 登录视图
     */
    public function index()
    {
        // 用户名密码赋值
        // if(cookie('login_username') && cookie('login_password')) {
        //     $this->show('username', cookie('login_username'));
        //     $this->show('password', cookie('login_password'));
        //     $this->show('remember', cookie('login_remember'));
        // }

        $this->show('system_name', $this->config['SYSTEM_NAME']);
        $this->show('copyright_info', $this->config['COPYRIGHT_INFO']);
        $this->show('company_name', $this->config['COMPANY_NAME']);
        $this->show('company_url', $this->config['COMPANY_URL'] ? $this->config['COMPANY_URL'] : 'javascript:;');
        $this->view();
    }

    /**
     * 登录验证
     */
    public function verify()
    {
        // 判断是否是submit
        is_submit();

        $username = post('username', '');
        if(empty($username)) {
            json(['status' => 0, 'info' => '登录账号为空', 'code' => 1]);
        }

        $password = post('password', '');
         if(empty($password)) {
            json(['status' => 0, 'info' => '登录密码为空', 'code' => 2]);
        }

        // 用户验证
        $userinfo = $this->config['SYSTEM_USER'][$username];
        if(!isset($userinfo)) {
            json(['status' => 0, 'info' => '帐号或密码错误1', 'code' => 3]);
        }
        if($password != $userinfo['password']) {
            json(['status' => 0, 'info' => '帐号或密码错误2', 'code' => 4]);
        }

        session('sess_userrole', $username);
        session('sess_truename', $userinfo['truename']);
        // 获取用户权限树
        session('sess_roletree', $userinfo['roletree']);

        // 记住用户名和密码
        // $remember = !empty($_POST['remember']) ? 1 : 0;
        // if($remember) {

        //     cookie('username',$username,array('expire'=>259200,'prefix'=>'login_'));
        //     cookie('password',$password,array('expire'=>259200,'prefix'=>'login_'));
        //     cookie('remember',1,array('expire'=>259200,'prefix'=>'login_'));

        // } else {
        //     // 删除cookie
        //     cookie(null,'login_');
        // }

        // 获取用户跳转URL
        $url = get_jumpurl_by_roletree(session('sess_roletree'));
        // pt($url);
        // 跳转后台首页
        json(['status' => 1, 'info' => '登录成功', 'url' => $url]);
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        session(null);
        location_url('/admin/login/index');
    }

}